﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace theRift
{
    public class MenuTextManager : Microsoft.Xna.Framework.DrawableGameComponent
    {
        protected SpriteBatch spriteBatch = null;
        protected Vector2 position = new Vector2();
        protected readonly SpriteFont menuFont, menuSelectedFont;
        protected Color menuColor = Color.White, menuSelectedColor = Color.Blue;
        protected int selectedIndex = 0;
        private readonly StringCollection menuItems;
        protected int width, height; // menu size

        protected KeyboardState oldKeyboardState;

        public MenuTextManager(Game game, SpriteFont menuFont, SpriteFont menuSelectedFont)
            : base(game)
        {
            this.menuFont = menuFont;
            this.menuSelectedFont = menuSelectedFont;
            menuItems = new StringCollection();

            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof (SpriteBatch));

            oldKeyboardState = Keyboard.GetState();
        }


        public void SetMenuItems(string[] items)
        {
            menuItems.Clear();
            menuItems.AddRange(items);
            SetMenuArea();
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Color MenuColor
        {
            get { return menuColor; }
            set { menuColor = value; }
        }

        public Color MenuSelectedColor
        {
            get { return menuSelectedColor; }
            set { menuSelectedColor = value; }
        }

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; }
        }

        public int Width
        {
            get { return width; }
        }

        public int Height
        {
            get { return height; }
        }

        protected void SetMenuArea()
        {
            width = 0;
            height = 0;

            foreach (string item in menuItems)
            {
                Vector2 size = menuSelectedFont.MeasureString(item);

                if (size.X > width)
                {
                    width = (int)size.X;
                }

                height = menuSelectedFont.LineSpacing;
            }
        }

        public override void Update(GameTime gameTime)
        {
            KeyboardState keyboardState = Keyboard.GetState();
            bool down, up;

            // Keyboard handling
            down = (oldKeyboardState.IsKeyDown(Keys.Down) && (keyboardState.IsKeyUp(Keys.Down)));
            up = (oldKeyboardState.IsKeyDown(Keys.Up) && (keyboardState.IsKeyUp(Keys.Up)));

            if (down)
            {
                selectedIndex++;

                if (selectedIndex == menuItems.Count)
                    selectedIndex = 0;
            }

            if (up)
            {
                selectedIndex--;

                if (selectedIndex == -1)
                    selectedIndex = menuItems.Count - 1;
            }

            oldKeyboardState = keyboardState;

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred,
                SaveStateMode.SaveState);

            for (int i = 0; i < menuItems.Count; i++)
            {
                SpriteFont font;
                Color color;

                if (i == selectedIndex)
                {
                    font = menuSelectedFont;
                    color = menuSelectedColor;
                }
                else
                {
                    font = menuFont;
                    color = menuColor;
                }

                // Shadow
                spriteBatch.DrawString(font, menuItems[i], new Vector2(position.X + 1, position.Y + 1 + (height * i)), Color.Black);

                // Text
                spriteBatch.DrawString(font, menuItems[i], new Vector2(position.X, position.Y + (height * i)), color);
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
